import random
import uuid
import requests
from typing import List, Dict
import json

def get_all_users(base_url: str, token: str) -> List[Dict]:
    """获取所有用户"""
    url = f"{base_url}/api/v1/user/get_all_users"
    headers = {
        'Authorization': f'Bearer {token}',
        'Content-Type': 'application/json'
    }
    
    users = []
    try:
        # 这里假设你有一个获取所有用户的接口
        # 如果没有，可能需要通过其他方式获取用户列表
        response = requests.get(url, headers=headers)
        if response.status_code == 200:
            users = response.json()
            # 过滤掉admin用户
            users = [user for user in users if user['username'] != 'admin']
    except Exception as e:
        print(f"获取用户列表失败: {str(e)}")
    
    return users

def create_random_relations(base_url: str, token: str, users: List[Dict], num_relations: int = 50):
    """创建随机用户关系"""
    url = f"{base_url}/api/v1/user/create_relation"
    headers = {
        'Authorization': f'Bearer {token}',
        'Content-Type': 'application/json'
    }
    
    success_count = 0
    created_pairs = set()  # 用于追踪已创建的关系对
    
    for i in range(num_relations):
        try:
            # 随机选择两个不同的用户
            user1, user2 = random.sample(users, 2)
            
            # 确保不重复创建相同的关系
            pair = (user1['id'], user2['id'])
            if pair in created_pairs:
                continue
                
            # 随机选择关系类型
            relation_type = random.choice([1, 2, 3])  # 1-父子关系 2-母子关系 3-夫妻关系
            
            parent_id = user1['id']
            child_id = user2['id']
            
            relation_data = {
                "parent_id": parent_id,
                "child_id": child_id,
                "relation_type": relation_type,
                "family": "e9d54447-3fa9-4018-9058-e2bffb764cbc",
                "remarks": f"Random relation {relation_type}"
            }
            
            response = requests.post(url, json=relation_data, headers=headers)
            
            if response.status_code == 200:
                success_count += 1
                created_pairs.add(pair)
                print(f"成功创建关系 {i+1}: User{parent_id} -> User{child_id} (类型: {relation_type})")
            else:
                print(f"创建关系 {i+1} 失败: {response.status_code}")
                print(response.json())
                
        except Exception as e:
            print(f"创建关系 {i+1} 时发生错误: {str(e)}")
    
    print(f"\n总结: 成功创建 {success_count} 个关系")

if __name__ == "__main__":
    BASE_URL = "http://172.16.1.188:18000"
    TOKEN = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VybmFtZSI6ImFkbWluIiwidXNlcl9lbWFpbCI6Ind1d2VudDkzMEAxNjMuY29tIiwidXNlcl9waG9uZSI6IjE4MjI1ODg4MzgxIiwiZXhwIjoxNzM3NTY0ODE0fQ.Hgp6gKATPq10zxsQrRG6dyONWfRxr_oXFjSPdZtpiUk"  # 替换为实际的token
    
    # 获取所有用户
    users = get_all_users(BASE_URL, TOKEN)
    if users:
        print(f"获取到 {len(users)} 个用户")
        # 创建随机关系，数量可以调整
        create_random_relations(BASE_URL, TOKEN, users, num_relations=50)
    else:
        print("没有获取到用户数据") 